package com.A01.sys.controller;

import java.util.Arrays;
import java.util.Map;

import com.A01.sys.entity.RecruitEntity;
import com.A01.sys.entity.UserEntity;
import com.A01.sys.service.RecruitService;
import com.A01.utils.ContextHolderUtils;
import com.A01.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.A01.utils.PageUtils;
import com.A01.utils.R;



/**
 *  招聘信息控制器
 *
 */
@RestController
@RequestMapping("dev/recruit")
public class RecruitController {
    @Autowired
    private RecruitService recruitService;

    /**
     * 根据用户信息，打开列表。
     * 将映射类型的参数params，传入招聘服务层的查询页方法中。
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        PageUtils page = recruitService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 公司发布的
     */
    @RequestMapping("/myCreatePage")
    public R myCreatePage(@RequestBody Map<String, Object> params){
        PageUtils page = recruitService.myCreatePage(params);
        return R.ok().put("page", page);
    }

    /**
     * 招聘信息的条件查询
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        RecruitEntity recruit = recruitService.selectById(id);

        return R.ok().put("recruit", recruit);
    }

    /**
     * 保存在系统中创建的新招聘信息。
     */
    @RequestMapping("/save")
    public R save(@RequestBody RecruitEntity recruit){
        UserEntity user = ContextHolderUtils.getUser();
        recruit.setCompanyId(user.getCompanyId());
        recruit.setUserId(user.getUserId());
        if(recruit.getCreateDate()==null){
            throw new RRException("发布日期不能为空");
        }
        if(recruit.getDescribes()==null){
            throw new RRException("岗位描述不能为空");
        }
        recruitService.insert(recruit);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody RecruitEntity recruit){
        UserEntity user = ContextHolderUtils.getUser();
        recruit.setCompanyId(user.getCompanyId());
        recruitService.updateAllColumnById(recruit);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        recruitService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
